package com.itheima.test;


import com.itheima.domain.Student2;

import java.util.TreeSet;

public class ComparatorDemo {
    public static void main(String[] args) {
//        TreeSet<Student2> treeSet=new TreeSet<Student2>(new Comparator<Student2>() {
//            @Override
//            public int compare(Student2 o1, Student2 o2) {
//                int i = o1.getAge() - o2.getAge();
//                int i1 = i == 0 ? o1.getName().compareTo(o2.getName()) : i;
//                return i1;
//            }
//        });

        TreeSet<Student2> treeSet=new TreeSet<Student2>((Student2 o1, Student2 o2)->
        {    int i = o1.getAge() - o2.getAge();
            int i1 = i == 0 ? o1.getName().compareTo(o2.getName()) : i;
            return i1;} );

        Student2 student=new Student2("yi",5);
        Student2 student1=new Student2("er",7);
        Student2 student2=new Student2("san",3);
        Student2 student3=new Student2("si",3);
        Student2 student4=new Student2("wu",8);
        //ctrl+alt+T  if。。。catch的快捷键
        treeSet.add(student);
        treeSet.add(student1);
        treeSet.add(student2);
        treeSet.add(student3);
        treeSet.add(student4);

        for (Student2 s:treeSet){
            System.out.println(s.getName()+" "+s.getAge());

        }
    }
}
